package com.sqx.modules.errand.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.errand.entity.Indent;
import com.sqx.modules.errand.entity.SysComplaint;
import com.sqx.modules.errand.service.ComplaintService;
import com.sqx.modules.errand.service.IndentService;
import com.sqx.modules.errand.vo.BasicsVo;
import com.sqx.modules.errand.vo.IndentVo;
import com.sqx.modules.utils.EasyPoi.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/indent")
@Api(value = "订单", tags = {"订单"})
public class AdminIndentController {
    @Autowired
    private IndentService indentService;
    @Autowired
    private ComplaintService complaintService;

    @PostMapping(value = "accomplishOrders")
    @ApiOperation("完成订单")
    public Result accomplishOrders(Long indentId) {
        Indent indent = indentService.getById(indentId);
        if(indent.getRiderMoney()==null){
            return Result.error("师傅还未抵达终点");
        }
        return indentService.indentPayCallback(indentId, 0, indent.getIndentMoney());
    }

    @PostMapping(value = "basicsMoney")
    @ApiOperation("计算订单基础价格")
    public Result basicsMoney(Long userId, BasicsVo basicsVo) {

        return Result.success().put("data", indentService.basicsMoney(userId, basicsVo));
    }

    @PostMapping(value = "addIndent")
    @ApiOperation("管理端添加任务")
    public Result addIndentBuy(@RequestBody Indent indent) {
        return indentService.addIndentBuy(indent);
    }

    @PostMapping(value = "distance")
    @ApiOperation("根据经纬度计算两点之间距离")
    public Result distance(Double ol, Double od, Double dl, Double dd, Integer type) {


        return indentService.distance(ol, od, dl, dd, type);
    }

    @PostMapping(value = "updateIndent")
    @ApiOperation("修改订单")
    public Result updateIndent(@RequestBody Indent indent) {
        return indentService.updateById(indent) ? Result.success() : Result.error();
    }


    @ApiOperation("订单列表--导出")
    @GetMapping("/indentExcelOut")
    public void indentExcelOut(String indentType, String indentNumber, String indentState, String phone, String date, String dateType, String startTime, String endTime, String parentName, String parentParentName, HttpServletResponse response) throws IOException {
        List<IndentVo> list = indentService.indentExcelOut(indentType, indentNumber, indentState, phone, date, dateType, startTime, endTime, parentName, parentParentName, response);
        ExcelUtils.exportExcel(list, "订单统计表", "订单统计Sheet", IndentVo.class, "订单统计表", response);
    }

    @ApiOperation("审核用户投诉")
    @GetMapping(value = "updateCustomerComplaint")
    public Result updateCustomerComplaint(Long complaintId, Integer complaintState) {

        return complaintService.updateCustomerComplaint(complaintId, complaintState);
    }

    //审核骑手申诉
    @ApiOperation("是否通过骑手申诉")
    @PostMapping(value = "updateComplaintState")
    public Result updateComplaintState(@RequestBody SysComplaint sysComplaint) {

        return complaintService.updateComplaintState(sysComplaint);
    }

    @GetMapping("/getProfitList")
    @ApiOperation("师傅收益排行榜")
    public Result getProfitList(Integer page, Integer limit, String time, Integer flag) {
        return Result.success().put("data", indentService.getProfitList(page, limit, time, flag));
    }

}
